package com.tanjun.userservice.web.controller.admin;

import com.tanjun.common.annotation.Edit;
import com.tanjun.common.model.R;
import com.tanjun.userservice.apiservice.request.menu.AdminMenuRequest;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuResponse;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuRouteResponse;
import com.tanjun.userservice.apiservice.response.adminmenu.admin.AdminMenuTreeResponse;
import com.tanjun.userservice.web.biz.admin.AdminMenuBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tanjun.common.constants.UrlConstant.URL_AUTH.AUTH;
import static com.tanjun.common.constants.UrlConstant.URL_AUTH.SYSTEM;
import static com.tanjun.common.constants.UrlConstant.URL_USER.ADMIN;

@RestController
@RequestMapping(ADMIN+"/adminMenu")
@Slf4j
public class BaseAdminMenuController {
    @Autowired
    private AdminMenuBiz adminMenuBiz;

    /**
     * 获取树形结构
     * @return
     */
    @PostMapping(SYSTEM+"/listTree")
    public R<List<AdminMenuTreeResponse>> listTree(){
        return R.OK(adminMenuBiz.listTree());
    }

    /**
     * 获取树形结构
     * @return
     */
    @GetMapping(SYSTEM+"/tree")
    public R<List<AdminMenuTreeResponse>> tree(@RequestParam(required = false,defaultValue = "false")Boolean includeButton){
        return R.OK(adminMenuBiz.tree(includeButton));
    }

    /**
     * 删除菜单
     * @param menuId 菜单id
     * @return 是否成功
     */
    @GetMapping(SYSTEM+"/getMenuById")
    public R<AdminMenuResponse> getMenuById(@RequestParam("menuId")Long menuId){
        return R.OK(adminMenuBiz.getMenuById(menuId));
    }

    /**
     * 新增菜单
     * @param paramVo 菜单数据
     * @return 是否成功
     */
    @PostMapping(SYSTEM+"/add")
    public R addMenu(@RequestBody @Validated AdminMenuRequest paramVo){
        adminMenuBiz.addMenu(paramVo);
        return R.OK();
    }

    /**
     * 修改菜单
     * @param paramVo 菜单数据
     * @return 是否成功
     */
    @PostMapping(SYSTEM+"/update")
    public R updateMenu(@RequestBody @Validated(Edit.class) AdminMenuRequest paramVo){
        adminMenuBiz.updateMenu(paramVo);
        return R.OK();
    }

    /**
     * 删除菜单
     * @param menuId 菜单id
     * @return 是否成功
     */
    @GetMapping(SYSTEM+"/del")
    public R delMenu(@RequestParam("menuId")Long menuId){
        adminMenuBiz.delMenuById(menuId);
        return R.OK();
    }



    @GetMapping(AUTH+"/getMenuRoutes")
    public R<List<AdminMenuRouteResponse>> getRoutes(){
        return R.OK(adminMenuBiz.getRoutes());
    }
}
